package com.jiezheng.huinong.action;

import com.jiezheng.huinong.constant.CommonConstants;
import com.jiezheng.huinong.dao.UserManageDAOImpl;
import com.jiezheng.huinong.model.UserInfo;
import com.opensymphony.xwork2.ActionSupport;

public class UserManageAction extends ActionSupport {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 3389517329092078382L;

	private UserInfo user = new UserInfo();

	private boolean userExist = false;

	/**
	 * 添加用户
	 * 
	 * @return
	 */
	public String addUser() {

		// 获取登录编号
		String prefix = user.getDeptNo() + user.getPostNo();
		Integer maxSn = (new UserManageDAOImpl()).getMaxSn(prefix);

		// 经办岗
		if (CommonConstants.WORKER.equals(user.getPostNo())) {
			maxSn = maxSn + 1;
			// 动态生成新用户编号
			String newLoginId = String.format("%s%02d", prefix, maxSn);

			user.setLoginId(newLoginId);
			user.setSn(maxSn);
		}

		// 非经办岗，只能有一个用户判断其存在性
		else {
			user.setLoginId(prefix);
			UserInfo userIn = (new UserManageDAOImpl()).getUserByLoginId(user
					.getLoginId());
			if (userIn != null) {
				userExist = true;
				return SUCCESS;
			}
		}

		// 插入数据
		(new UserManageDAOImpl()).addUser(user);
		return SUCCESS;
	}

	/**
	 * 删除用户
	 */
	public String deleteUser() {
		(new UserManageDAOImpl()).deleteUser(user);
		return SUCCESS;
	}

	/**
	 * 修改用户表单
	 */
	public String preModifyUser() {
		user = (new UserManageDAOImpl()).getUserByLoginId(user.getLoginId());
		return SUCCESS;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 */
	public String modifyUser() {
		(new UserManageDAOImpl()).modifyUser(user);
		return SUCCESS;
	}

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

	public boolean isUserExist() {
		return userExist;
	}

	public void setUserExist(boolean userExist) {
		this.userExist = userExist;
	}
}
